<?php 
$GLOBALS['Md'] = "Clients";
$GLOBALS['Pd'] = "N";
require_once($_SERVER['DOCUMENT_ROOT']."/defaultconf.mex");
require_once($GLOBALS['BIB']."Objects/obj.mssql.php");
print heade(); 
print body(); 
module($BSD['DB00']); 
office_header($_SESSION[$SESSOF]);
include($GLOBALS['PAD']."Modules/Clients/nav.views.php");
print NavFront();
?>
<script charset="utf-8" type="text/javascript" src="../../../Shared/Libraries/JS/calendar.js"></script>
<div id="Centro" class="strcture">
<?php
include($GLOBALS['PAD']."Modules/Clients/main.obj.php");

if (isset($_POST['QID'])) {
	
	$CSave = "INSERT INTO outbound (CLIENT, QUESTION, VALUE, VALUE001) VALUES ('{$_POST['CLIENT']}','{$_POST['QID']}','{$_POST['VALUE']}','{$_POST['VALUE001']}')";
	$Save = $GLOBALS['CONN']->db_query($CSave);
} 
 if (!isset($_POST['DATES'])) {
 $_POST['DATES'] = date("Y-01-d");
 }
 if (!isset($_POST['DATET'])) {
 $_POST['DATET'] = date("Y-04-d");
 }
?>

<table width="100%"  border="0" cellpadding="4" cellspacing="2">
  <?php if(isset($_GET['action']) && $_GET['action'] == "new") { ?>
  <tr>
    <td><table width="100%"  border="0" cellpadding="10" cellspacing="1">
      <tr bgcolor="#009999">
        <td colspan="3">Contacto ao Cliente </td>
      </tr>
	<?php echo Obj_Create_Client();?>
    </table> </td>
  </tr>
  <?php } ?>
  <tr>
    <td>Pesquisar cliente

<table width="100%"  border="0" cellpadding="10" cellspacing="3">
	<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" runat="server">
        <tr bgcolor="#F2F2F2">
          <td width="80%"><div align="right">
            <select name="select">
              <option>Sem efectuar remessas</option>
            </select> 
            Entre 
            <?php
		$GLOBALS['MYT'] = $GLOBALS['CONN'];
		mn_field('text','ED','DATES',$_POST['DATES'],'10','8','False',''); ?>
            e <?php
		mn_field('text','ED','DATET',$_POST['DATET'],'10','8','False',''); ?> 
            Loja 
		<?php
		if ($_SESSION[$SESSOF] == 1) {
			$Off = 'WHERE Type IN (1,2,5)';
		} else {
			 $Off = 'WHERE ref = '.$_SESSION[$SESSOF];
		}
		$COff = "SELECT office_number AS ID, name AS NAME FROM main_offices ".$Off;
		mn_field('select','ED','Offices','811',$COff,'','False','" readonly="'); ?>
          </div></td>
          <td width="20%">
		  <input name="Search" type="submit" id="Search" value="Pesquisar">
          </td>
		  
        </tr>
	
</table></td>
  </tr>
  <?php if(isset($_POST['Search']) && !isset($_GET['ACT'])) { ?>
  <tr>
    <td>Resultados      <table width="100%"  border="0" cellpadding="2" cellspacing="1">
          <tr bgcolor="#009999">
		    <td></td>
            <td>N&uacute;mero</td>
            <td>Nome</td>
            <td bgcolor="#009999">Data &Uacute;ltima </td>
            <td bgcolor="#009999">Localidade</td>
            <td>Telefone 1 </td>
            <td>Telefone 2 </td>
          </tr>
 	<?php 
	LigarMSBSD($BSD['DB01']);
	$dt = split('-',$_POST['DATET']);
	$dtm = $dt[2].'-'.$dt[1].'-'.$dt[0];
	$dte = split('-',$_POST['DATES']);
	$dtem = $dte[2].'-'.$dte[1].'-'.$dte[0];
	$QCall = "SELECT     
				OriNombre,Clientes.CliNumero, 
				Clientes.CliNombre + ' ' + Clientes.CliApellidos AS Remitente,
				CliTelefono,
				CliTelefono2,
				CliProvincia,
				COUNT(FraId) AS GIROS,
				MAX(FraFechaAlb) AS UltimoEnvio
			FROM Facturas 
		INNER JOIN  Clientes ON Clientes.CliNumero = Facturas.FraCliNumero  
		INNER JOIN Origenes ON OriNumero =FraOrigen0
			WHERE Facturas.FraFechaAlb < '{$dtm}'
			AND Facturas.FraFechaAlb > '{$dtem}'
			AND Clientes.CliNumero NOT IN
                 (SELECT FraCliNumero
                      FROM Facturas
                     WHERE FraFechaAlb > '{$dtm}'
					 GROUP BY FraCliNumero)
			AND FraOrigen0 = {$_POST['Offices']}
			GROUP BY OriNombre,Clientes.CliNumero, 
					Clientes.CliNombre + ' ' + Clientes.CliApellidos,
					CliTelefono,CliTelefono2,CliProvincia
			ORDER BY UltimoEnvio";
	$Call = $GLOBALS['MSCONN']->db_array($QCall);
	print $Call[0];
	for ($i = 1; $i <= $Call[0]; $i++) { 
	
	 $VROT = $GLOBALS['CONN']->db_array("SELECT REF FROM outbound WHERE client = {$Call[$i]['CliNumero']}");
	 if (isset($VROT[1])) {
	 $Ctt = '<img src="'.$GLOBALS['GFX'].'true.png" alt="j� foi contactado">';
	 } else {
	 $Ctt = '';
	 }
	?>

          <tr bgcolor="#F2F2F2">
		  <td><?php echo $Ctt;?></td>
            <td width="20%"><a href="<?php echo $_SERVER['PHP_SELF'];?>?ACT=<?php echo $Call[$i]['CliNumero'];?>"><?php echo $Call[$i]['CliNumero'];?></a></td>
            <td width="40%"><?php echo $Call[$i]['Remitente'];?></td>
            <td width="20%" bgcolor="#F2F2F2"><?php echo $Call[$i]['UltimoEnvio'];?></td>
            <td width="20%" bgcolor="#F2F2F2"><?php echo $Call[$i]['CliProvincia'];?></td>
            <td width="20%"><?php echo $Call[$i]['CliTelefono'];?></td>
            <td width="20%"><?php echo $Call[$i]['CliTelefono2'];?></td>
        </tr>
		<?php } ?>
      </table>
	<?php } elseif (isset($_GET['ACT'])) {?>
      <table width="100%"  border="0" cellspacing="1" cellpadding="3">
        <tr bgcolor="#DDDDDD">
          <td colspan="2">Questionário</td>
          </tr>
        <tr>
          <td>Cliente:</td>
          <td><?php
		  echo mn_field('text','ED','CLIENT',$_GET['ACT'],'','','False','" readonly="');
		  ?></td>
        </tr>
        <tr>
          <td>Pergunta:</td>
          <td><?php $OTQUEST = $GLOBALS['MYT']->db_array('SELECT REF, TEXT FROM outbound_questions');
		  			for ($r = 1; $r <= $OTQUEST[0]; $r++) { 
					echo mn_field('hidden','ED','QID',$OTQUEST[$r]['REF'],'','','False','');
					print $OTQUEST[$r]['TEXT']; ?>
					</td>
          </tr>
        <tr>
          <td>Resposta:</td>
          <td><?php
		$OTAns = "SELECT REF AS ID, NAME AS NAME FROM outbound_values WHERE QUESTION = {$OTQUEST[$r]['REF']}";
		mn_field('select','ED','VALUE','',$OTAns,'','False',''); ?>
		  <?php } ?>Observ:
		  <input name="VALUE001" type="text" size="50" maxlength="255"></td>
          </tr>
      </table>
      <p>
        <input name="Search2" type="submit" id="Search2" value="Guardar">
      </p></td>
  </tr>
  <?php } ?>
  </form>
</table>
</div>
<?php echo footer(); ?>